<template>
  <el-aside :width="$store.state.isCollapse ? '180px' : '64px'">
    <el-menu class="el-menu-vertical-demo" background-color="#558f93 " text-color="#fff"
      :collapse="!$store.state.isCollapse" :collapse-transition="false">

      <h3 v-show="$store.state.isCollapse">
        <div style="margin-top: 20px; margin-bottom: 10px; padding-right: 10px; padding-left: 10px;"><img
            src="../assets/images/logo.png" :style="{ width: '100%', height: 'auto' }"></div>
      </h3>


      <el-sub-menu :index="item.label" v-for="item in hasChildren()" :key="item.path">
        <template #title>
          <component class="icons" :is="item.icon"></component>
          <span>{{ item.label }}</span>
        </template>
        <el-menu-item-group>
          <el-menu-item :index="subIndex.path" v-for="(subItem, subIndex) in item.children" :key="subIndex"
            @click="clickMenu(subItem)">
            <component class="icons" :is="subItem.icon"></component>
            <span>{{ subItem.label }}</span>
          </el-menu-item>
        </el-menu-item-group>
      </el-sub-menu>

      <el-menu-item :index="item.path" v-for="item in noChildren()" :key="item.path" @click="clickMenu(item)">
        <component class="icons" :is="item.icon"></component>
        <span>{{ item.label }}</span>
      </el-menu-item>


    </el-menu>
  </el-aside>
</template>

<script>
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
export default {
  setup() {
    const store = useStore()
    const list = [
      {
        path: '/user',
        name: 'user',
        label: '学校管理',
        icon: 'user',
        url: 'UserManage/UserManage',
        children: [
          {
            path: '/page1',
            name: 'page1',
            label: '学年管理',
            url: 'Other/PageOne'
          },
          {
            path: '/page2',
            name: 'page2',
            label: '教师管理',
            url: 'Other/PageTwo'
          },
          {
            path: '/page3',
            name: 'page3',
            label: '日志',
            url: 'Other/PageThree'
          }
        ]
      },
      {
        path: '/other',
        name: 'other',
        label: '资料管理',
        // icon: 'location',
        icon: 'FolderOpened',
      }
    ];

    const router = useRouter();

    const noChildren = () => {
      return list.filter((item) => !item.children);
    };

    const hasChildren = () => {
      return list.filter((item) => item.children);
    };

    const clickMenu = (item) => {
      router.push({
        name: item.name,
      });
      //用vuex 来管理
      store.commit('selectMenu', item);
    };

    return {
      noChildren,
      hasChildren,
      clickMenu,
    }
  },
}
</script>

<style scoped>
.icons {
  width: 18px;
  height: 18px;
}

/deep/.el-menu {
  background-color: #558aa5;
  border-right: none;
}
</style>